from mininet.topo import Topo
from mininet.net import Mininet
from mininet.node import Node
from mininet.log import setLogLevel, info
from mininet.cli import CLI


class Router(Node):
    def config(self, **params):
        super(Router, self).config(**params)
        # Enable forwarding on the router
        self.cmd('sysctl net.ipv4.ip_forward=1')

    def terminate(self):
        self.cmd('sysctl net.ipv4.ip_forward=0')
        super(Router, self).terminate()


class MyTopo(Topo):
    def build( self, *args, **params ):
        r1 = self.addNode('r1', cls=Router)
        r2 = self.addNode('r2', cls=Router)
        h1 = self.addHost('h1')
        h2 = self.addHost('h2')
        self.addLink(r1, r2)
        self.addLink(h1, r1)
        self.addLink(h2, r2)

topo = MyTopo()
net = Mininet(topo=topo)
net.start()
CLI(net)
net.stop()